<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.HisiPHP.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------

namespace app\system\admin;  

use app\system\model\SystemDaili as DailiModel;
use think\Db;
/**
 * 代理管理控制器
 * @package app\system\admin
 */
class Daili extends Admin
{ 
    protected $hisiTable = 'Daili';
    /**
     * 代理等级
     */
    public function level(){ 
        $dl_model = new DailiModel();
        $level_list = $dl_model->getLevelList();
        $this->assign('level_list', $level_list); 
        return $this->fetch();
    }
    /**
     * 代理等级-编辑
     */
    public function levelEdit(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $dl_model = new DailiModel();
            if (!$dl_model->levelUpdate($data)) {
                return $this->error('修改失败');
            }
            return $this->success('修改成功'); 
        }else{
            $dl_model = new DailiModel();
            $id = $this->request->param('id');
            $level_info = $dl_model->getLevelInfo($id);
            $this->assign('level_info', $level_info);
            return $this->fetch();
        } 
    }
    
    public function index(){
        $dl_model = new DailiModel();
        $daili_list = $dl_model->getDailiList("sqh != ''");
        $this->assign('daili_list', $daili_list); 
        return $this->fetch();
    }
    
    public function indexDsh(){
        $dl_model = new DailiModel();
        $daili_list = $dl_model->getDailiList("sh_status = 0");
        $this->assign('daili_list', $daili_list);
        return $this->fetch();
    }
    
    public function shenheDaili(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $daili_info = Db::name('daili')->where("id = '{$data['id']}'")->find();
             if($daili_info['sh_status'] == 1){
						 return $this->success('审核成功',url('daili/index')); 
             } 
            $dl_model = new DailiModel();
            if (!$dl_model->shenheDaili($data)) {
                return $this->error('审核失败');
            }
            
            
            $datas = array(
                'first' => array('value'=>urlencode("恭喜您！您的代理申请已经通过审核！")),
                'keyword1' => array('value' => urlencode($daili_info['dlname'])),
                'keyword2' => array('value' => urlencode("审核通过")),
                'remark' => array('value' => "点击登录系统"),
            );
            $this->template_reserve($daili_info['openid'], "http://daili.huyingbaoxt.com/", $datas,'LRNleOpJ4VNFveI48Ko58_5h_goPpoemzwOxUgsp7V0');
            
            $datas1 = array(
                'first' => array('value'=>urlencode("恭喜您，有一名代理加入到您的团队")),
                'keyword1' => array('value' => urlencode($daili_info['dlname'])),
                'keyword2' => array('value' => urlencode($daili_info['dl_weixin'])),
                'keyword3' => array('value' => urlencode($daili_info['dl_phone'])),
                'remark' => array('value' => "请您及时和代理联系"),
            );
            $fjdl_info = Db::name('daili')->where("id = '{$daili_info['pid']}'")->find();
            $this->template_reserve($fjdl_info['openid'], "http://daili.huyingbaoxt.com/", $datas1,'hslhwkFDTwWm8GJeTQfeBQSazmxqxNv6Be8r2cEnYVM');
            
            
            return $this->success('审核成功',url('daili/index')); 
        }else{
            $dl_model = new DailiModel();
            $id = $this->request->param('id');
            $daili_info = $dl_model->getDailiInfo($id);
            $this->assign('daili_info', $daili_info);
            return $this->fetch();
        }
    }
    
    /**
     * 预约模板消息
     * @param str $touser
     * @param str $dhjp
     * @param str $kjsj
     * @param str $turl
     * @return unknown
     */
    public function template_reserve($touser,$turl,$data,$tpid){
        $json_token = $this->http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd232643ac5db648b&secret=160deec51168bd49413a6ec58fa3804b");
        
        $access_token = json_decode($json_token,true);
        //获得access_token
        $maccess_token = $access_token['access_token'];
        $template=array(
            'touser' => $touser,
            'template_id' => $tpid,
            'url' => $turl,
            'topcolor' => "#7B68EE",
            'data'=> $data
        );
        $json_template = json_encode($template);
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$maccess_token;
        $res = $this->http_request($url,urldecode($json_template));
        return $res;
    }
    
    function http_request($url,$data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    
    /**
     * 代理详情
     */
    public function dlxq(){
        $dl_model = new DailiModel();
        if($this->request->isPost()){
            $data = $this->request->post();  
            if (!$dl_model->updateDaili($data)) {
                return $this->error('更新失败');
            }
            return $this->success('更新成功',url('daili/index'));
        }else{
            $id = $this->request->param('id');
            $daili_info = $dl_model->getDailiInfo($id);
            
            $province = Db::name('city')->where("p_code = 0")->select();
            if($daili_info['province'] == 110000 || $daili_info['province'] == 500000 || $daili_info['province'] == 310000 || $daili_info['province'] == 120000){
                $city = Db::name('city')->where("code = '{$daili_info['province']}' and p_code = '{$daili_info['province']}'")->select();
            }else{
                $city = Db::name('city')->where("p_code = '{$daili_info['province']}'")->select();
            }
            $area = Db::name('city')->where("p_code = '{$daili_info['city']}'")->select();
            $this->assign("province",$province);
            $this->assign("city",$city);
            $this->assign("area",$area);
            
            $this->assign('daili_info', $daili_info);
            return $this->fetch();
        }
    }
    
    /**
     * 获取城市
     */
    public function getCity(){
        $data = $this->request->post();
        if($data['province'] == 110000 || $data['province'] == 500000 || $data['province'] == 310000 || $data['province'] == 120000){
            return Db::name('city')->where("code = '{$data['province']}' and p_code = '{$data['province']}'")->select();
        }else{
            return Db::name('city')->where("p_code = '{$data['province']}'")->select();
        }
    }
    
    /**
     * 获取地区
     */
    public function getArea(){
        $data = $this->request->post();
        return Db::name('city')->where("p_code = '{$data['city']}' and code != '{$data['city']}'")->select();
    }
    
    /**
     * 添加代理
     */
    public function dailiAdd(){
        return $this->fetch();
    }
}
